package com.moliku.insurance.common.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * API统一响应格式封装
 *
 * @author Mr.Liu
 * @since 2020/01/7
 */
@Getter
@Setter
@Accessors(chain = true)
@SuppressWarnings("unused")
@ApiModel(description = "返回信息")
@NoArgsConstructor
public class R<T> implements Serializable {
    private static final long serialVersionUID = -6903808263692675474L;

    @ApiModelProperty(value = "返回处理消息")
    private String message = "请求成功！";

    @ApiModelProperty(value = "返回代码")
    private Integer code = 200;

    @ApiModelProperty("承载数据")
    private Object data;

    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    private R(String message, Integer code) {
        this.message = message;
        this.code = code;
    }

    private R(String message, Integer code, T data) {
        this.message = message;
        this.code = code;
        this.data = data;
    }


    public R<T> pageData(IPage<T> data) {
        PageData pageData = new PageData();
        pageData.setPageSize(data.getSize());
        pageData.setPageNo(data.getCurrent());
        pageData.setTotalCount(data.getTotal());
        pageData.setTotalPage(data.getPages());
        List<T> records = CollectionUtils.isNotEmpty(data.getRecords()) ? data.getRecords() : Lists.newArrayList();
        pageData.setData(records);
        this.data = pageData;
        return this;
    }

    @Data
    public static class PageData {
        Long pageSize;
        Long pageNo;
        Long totalCount;
        Long totalPage;
        List<?> data;
    }

    public static <T> R<T> data(T data) {
        ResponseCode responseCode = ResponseCode.SUCCESS;
        return new R<T>(responseCode.getMessage(), responseCode.getCode(), data);
    }

    public static <T> R<T> success() {
        ResponseCode responseCode = ResponseCode.SUCCESS;
        return new R<T>(responseCode.getMessage(), responseCode.getCode());
    }

    public static <T> R<T> success(T data) {
        ResponseCode responseCode = ResponseCode.SUCCESS;
        return new R<T>(responseCode.getMessage(), responseCode.getCode(), data);
    }

    public static <T> R<T> success(T data, String message) {
        ResponseCode responseCode = ResponseCode.SUCCESS;
        return new R<T>(message, responseCode.getCode());
    }

    public static <T> R<T> success(String message, ResponseCode code) {
        return new R<>(message, code.getCode());
    }

    public static <T> R<T> fail() {
        ResponseCode responseCode = ResponseCode.FAILURE;
        return new R<T>(responseCode.getMessage(), responseCode.getCode());
    }

    public static <T> R<T> fail(String message) {
        ResponseCode responseCode = ResponseCode.FAILURE;
        return new R<T>(message, responseCode.getCode());
    }

    public static <T> R<T> fail(ResponseCode code) {
        return new R<>(code.getMessage(), code.getCode());
    }

    public static <T> R<T> fail(String message, int code) {
        return new R<>(message, code);
    }

    public static <T> R<T> fail(String message, ResponseCode code) {
        return new R<>(message, code.getCode());
    }

    public static <T> R<T> status(boolean flag) {
        return status(flag, "操作失败");
    }

    public static <T> R<T> status(boolean flag, String defaultErrorMsg) {
        ResponseCode responseCode = flag ? ResponseCode.SUCCESS : ResponseCode.FAILURE;
        String msg = flag ? responseCode.getMessage() : defaultErrorMsg;
        return new R<>(msg, responseCode.getCode());
    }

}
